package com.retirement.children.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.retirement.children.domain.OldChildren;
import com.retirement.children.dto.OldChildrenDto;
import com.retirement.children.mapper.OldChildrenMapper;
import com.retirement.children.service.IOldChildrenService;
import com.retirement.children.vo.OldChildrenVo;
import com.retirement.information.domain.OldInformation;
import com.retirement.information.mapper.OldInformationMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retirement.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijy
 * @since 2023-12-22
 */
@Service
public class OldChildrenServiceImpl extends ServiceImpl<OldChildrenMapper, OldChildren> implements IOldChildrenService {


    private final OldInformationMapper oldInformationMapper;

    public OldChildrenServiceImpl(OldInformationMapper oldInformationMapper) {
        this.oldInformationMapper = oldInformationMapper;
    }


    @Override
    public List<OldChildrenVo> getList(OldChildrenDto dto) {
        return baseMapper.getList(dto);
    }

    @Override
    public int addChildren(OldChildren dto) {
        Assert.isTrue(ObjectUtil.isNotNull(dto.getInformationId()), "无用户id信息");
        OldInformation bean = oldInformationMapper.selectOne(new LambdaQueryWrapper<OldInformation>()
                .eq(OldInformation::getId,dto.getInformationId())
                .eq(OldInformation::getDeleted,0));
        Assert.isTrue(ObjectUtil.isNotNull(bean), "无用户信息");
        dto.setId(IdUtil.simpleUUID());
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());

        return baseMapper.insert(dto);
    }

    @Override
    public int updateChildren(OldChildren dto) {
        OldChildren oldChildren = baseMapper.selectById(dto.getId());
        Assert.isTrue(ObjectUtil.isNotNull(oldChildren), "无此记录");
//        OldInformation bean = oldInformationMapper.selectOne(new LambdaQueryWrapper<OldInformation>()
//                .eq(OldInformation::getId,dto.getInformationId())
//                .eq(OldInformation::getDeleted,0));
//        Assert.isTrue(ObjectUtil.isNotNull(bean), "无用户信息");
        dto.setUpdateTime(LocalDateTime.now());

        return baseMapper.updateById(dto);
    }

    @Override
    public OldChildrenVo getDetail(String id) {
        OldChildren oldChildren = baseMapper.selectById(id);
        Assert.isTrue(ObjectUtil.isNotNull(oldChildren), "无此记录");
        OldInformation oldInformation = oldInformationMapper.selectById(oldChildren.getInformationId());
        Assert.isTrue(ObjectUtil.isNotNull(oldInformation), "无用户信息");
        OldChildrenVo oldChildrenVo = new OldChildrenVo();
        BeanUtils.copyProperties(oldChildren,oldChildrenVo);
        oldChildrenVo.setParentName(oldInformation.getName());

        return oldChildrenVo;
    }


}
